#pragma once
#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/socket.h>


class Util
{
public:
    static int ReadLine(int sock,std::string& out)
    {
        char ch='X';
        while(ch!='\n')
        {
            ssize_t n=recv(sock,&ch,1,0);
            if(n>0)
            {
                if(ch=='\r')
                {
                    recv(sock,&ch,1,MSG_PEEK);
                    if(ch=='\n')
                    {
                        recv(sock,&ch,1,0);
                    }else{
                        ch='\n';
                    }
                }
                out+=ch; 
            }else if(n==0)
            {
                //对端关闭
                return 0;
            }else
            {
                return -1;
            }
        }       
        return out.size();
    }

    static bool CutString(const std::string& target,std::string&sub1,std::string&sub2,const std::string& sep)
    {
        auto pos=target.find(sep);
        if(pos!=std::string::npos)
        {
            sub1=target.substr(0,pos);
            sub2=target.substr(pos+sep.size());
            return true;
        }
        return false;
    }
};